/**
 * 按获取的评论类型提取需要的数据
 */
const format = require('../tools/format');
format();

let fp = function (json) {
    let result = {};
    let fields = ['commentCount', 'defaultGoodCount',
        'score1Count', 'score2Count', 'score3Count', 'score4Count', 'score5Count',
        'videoCount', 'imageListCount', 'showCount', 'fixCount', 'afterCount'
    ];
    let summary = json.productCommentSummary;
    result.goodRate = summary.goodRate.toString();//float字段单独处理
    for (let f of fields) {
        if (summary[f]) {
            result[f] = summary[f];
        }
        else if (json[f]) {
            result[f] = json[f];
        }
    }
    if(json.comments&& json.comments.length>0){
        fp.comments_maxId = json.comments[0].id.toString();
    }
    return result;
};
/**
 * 对比score评论数量
 * @param {*} prev 
 * @param {*} curr 
 */
let scoreCompare = function (prev, curr) {
    let M_curr = new Map([
        [1, curr.score1Count],
        [2, curr.score2Count + curr.score3Count],
        [3, curr.score4Count + curr.score5Count]
    ]);
    let M_prev = new Map([
        [1, Number(prev.score1Count ? prev.score1Count : 0)],
        [2, Number(prev.score2Count ? prev.score2Count : 0) + Number(prev.score3Count ? prev.score3Count : 0)],
        [3, Number(prev.score4Count ? prev.score4Count : 0) + Number(prev.score5Count ? prev.score5Count : 0)]
    ]);
    let result = new Map();
    result.set(1, M_curr.get(1) - M_prev.get(1));
    result.set(2, M_curr.get(2) - M_prev.get(2));
    result.set(3, M_curr.get(3) - M_prev.get(3));
    return result;
}

/**
 * 获取评论主体
 * PS:不包括追评、图片、视频部分
 */
let ccBody = function (json) {
    let result = {};
    let fields = ['id', 'guid', 'content', 'creationTime',
        'replyCount', 'replyCount2', 'usefulVoteCount', 'productColor',
        'days', 'afterDays', 'integral', 'orderId', 'nickname',
        'referenceName', 'referenceTime', 'score', 'status', 'topped'
    ];
    for (let f of fields) {
        if (json[f]) {
            result[f] = json[f];
        }
    }
    if(json.afterUserComment && json.afterUserComment.id){
        result.afterUserComment_id = json.afterUserComment.id.toString();
        result.afterUserComment_commentId = json.afterUserComment.commentId.toString();
        result.afterUserComment_content = json.afterUserComment.content.toString();
        result.afterUserComment_modified = json.afterUserComment.modified;
        result.afterUserComment_created = json.afterUserComment.created;
    }
    result.create = new Date().format('yyyy-MM-dd HH:mm:ss');
    return result;
}

let ccReplies = function (json) {
    let replies = [];
    if(json.replies && json.replies.length>0){
        for (let r of json.replies) {
            replies.push({
                id: r.id.toString(),
                content: r.content,
                pin: r.pin,
                ip: r.ip,
                creationTime: r.creationTime,
                productId: r.productId.toString(),
                commentId: json.id.toString(),
                create:new Date().format('yyyy-MM-dd HH:mm:ss')
            });
        }
    }
    return replies;
}

let ccImages = function (json) {
    let images = [];
    if(json.images && json.images.length>0){
        for (let i of json.images) {
            images.push({
                id: i.id.toString(),
                imgUrl: i.imgUrl,
                commentId: json.id,
                after: 0,
                create:new Date().format('yyyy-MM-dd HH:mm:ss')
            });
        }
    }
    return images;
}

let ccVideos = function (json) {
    let videos = [];
    if(json.videos && json.videos.length>0){
        for (let v of json.videos) {
            videos.push({
                id: v.id.toString(),
                mainUrl: v.mainUrl,
                remark: v.remark,
                videoLength: v.videoLength,
                videoId: v.videoId,
                commentId: json.id,
                after: 0,
                create:new Date().format('yyyy-MM-dd HH:mm:ss')
            });
        }
    }
    return videos;
}


let ccAfter = function (json) {
    let after = {};
    after.afterUserComment_id = json.afterUserComment.id.toString();
    after.afterUserComment_commentId = json.afterUserComment.commentId.toString();
    after.afterUserComment_content = json.afterUserComment.content.toString();
    after.afterUserComment_modified = json.afterUserComment.modified;
    after.afterUserComment_created = json.afterUserComment.created;
    let replies = [], images = [], videos = [];
    if (json.replies && json.replies.length > 0) {
        for (let r of json.replies) {
            //replies没有追评之分
            replies.push({
                id: r.id.toString(),
                content: r.content,
                pin: r.pin,
                ip: r.ip,
                creationTime: r.creationTime,
                productId: r.productId.toString(),
                commentId: json.id.toString(),
                create:new Date().format('yyyy-MM-dd HH:mm:ss')
            });
        }
    }
    if (json.afterImages && json.afterImages.length > 0)//afterimage
    {
        for (let i of json.afterImages) {
            images.push({
                id: i.id.toString(),
                imgUrl: i.imgUrl,
                commentId: json.afterUserComment.id,
                after: 1,
                create:new Date().format('yyyy-MM-dd HH:mm:ss')
            });
        }
    }
    if (json.afterVideos && json.afterVideos.length > 0)//afterimage
    {
        for (let v of json.afterVideos) {
            videos.push({
                id: v.id.toString(),
                mainUrl: v.mainUrl,
                remark: v.remark,
                videoLength: v.videoLength,
                videoId: v.videoId,
                commentId: json.afterUserComment.id,
                after: 1,
                create:new Date().format('yyyy-MM-dd HH:mm:ss')
            });
        }
    }
    return {
        'body': after,
        'replies': replies,
        'images': images,
        'videos': videos
    };
}


module.exports.firstPage = fp;
module.exports.scoreCompare = scoreCompare;
module.exports.convertCommentBody = ccBody;
module.exports.convertCommentReplies = ccReplies;
module.exports.convertCommentImages = ccImages;
module.exports.convertCommentVideos = ccVideos;
module.exports.convertAfterComment = ccAfter;

